package com.renting.modules.renting.rentSys.web;

import java.util.List;

import cn.hutool.core.util.RandomUtil;
import com.renting.common.config.HttpStatusConstant;
import com.renting.common.entity.ResultList;
import com.renting.modules.renting.rentSys.dao.HouseDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.renting.common.entity.Result;
import com.renting.modules.renting.rentSys.entity.House;
import com.renting.modules.renting.rentSys.service.HouseService;


@Controller
public class HouseController {

    @Autowired
    private HouseService houseService;
    @Autowired
    private HouseDao houseDao;

    /**
     * houses
     *
     * @param house
     * @return
     * @description:得到房屋信息列表 如果没有查询条件得到所有的房屋信息（分页显示）
     * @exception:
     * @author administrator
     * @create date： 2018年3月10日 @remark：
     */
    @RequestMapping(value = "/v1/common/houses/id/{id}", method = RequestMethod.GET)
//	produces="application/json;charset=UTF-8"
    @ResponseBody
    public String houses(House house) {
        house = houseService.houses(house);
        if (house == null) {
            return Result.errorInfo("未找到房屋信息", "");
        }
        return JSONObject.toJSONString(house);
    }

    /**
     * getNearbyHouses
     *
     * @return
     * @description:公共接口 得到附近房源（默认5km以以内）
     * @exception:
     * @author administrator
     * @create date： 2018年3月16日 @remark：
     */
//    @RequestMapping(value = "/v1/common/houses/nearbyHouses", method = RequestMethod.GET)
//    @ResponseBody
//    public String getNearbyHouses(LocationEntity location) {
//        // 模拟数据 正式版本直接删掉 start
//        location = new LocationEntity("116.7519900000", "36.5535800000");
//        // 模拟数据 正式版本直接删掉 end
//        List<House> houses = houseService.getNearbyHouses(location);
//        ResultList<House> result = new ResultList<>();
//        result.setData(houses);
//        return JSONObject.toJSONString(result);
//    }
    @RequestMapping(value = "/v1/common/houses", method = RequestMethod.GET)
    @ResponseBody
    @CrossOrigin(origins = "*")
    public String getHouses(House house) {
        ResultList<House> resultList = new ResultList<>();
        int total = (int) houseDao.count();
        List<House> houses = houseService.getHouses(house);
        resultList.setTotal(total);
        resultList.setData(houses);
        return resultList.toJSONString();
    }


    @RequestMapping(value = "/v1/common/houses", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin(origins = "*")
    public Result addHouse(@RequestBody House house) {
        Result result = new Result(HttpStatusConstant.SUCCESS);
        if (house.getImages().size() == 0) {
            result.setMsg(HttpStatusConstant.INVALID_REQUEST, "图片不能为空");
            return result;
        }
        String id = RandomUtil.simpleUUID();
        house.setId(id);//生成一个id
        houseService.save(house);
        return result;
    }


}
